package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderListDao;
import com.itheima.domain.Order;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderListService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListDao orderListDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //忽略字符串里的空格
        if (queryString != null && queryString.length() > 0) {
            queryString = queryString.replaceAll(" ", "");
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<Map> page = orderListDao.findCondition(queryString);
        long total = page.getTotal();
        List<Map> result = page.getResult();
        try {
            for (Map map : result) {
                map.put("orderDate", DateUtils.parseDate2String((Date) map.get("orderDate")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageResult(total, result);
    }

    /**
     * 修改预约状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result update(Integer id, String status) {
        Order order = orderListDao.findById(id);
        if (order == null) {
            return new Result(false, "无法修改,该记录不存在");
        }
        if (order.getOrderStatus().equals("已到诊")) {
            return new Result(false, "无法修改,已是到诊状态");
        }
        if (order.getOrderStatus().equals("已取消")) {
            return new Result(false, "无法修改,该预约已被取消,请重新预约");
        }
        Map map = new HashMap();
        map.put("orderStatus", status);
        map.put("id", id);
        orderListDao.update1(map);
        return new Result(true, "修改成功!");
    }
}
